package com.hibernate.hrm.entity.dto.department;

import com.hibernate.hrm.entity.Department;
import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class DepartmentDTO implements Serializable {
    private Integer departmentID;
    private String departmentName;
    private String departmentCode;
    private String departmentHead;  // 负责人姓名
    private Boolean status;
    private Boolean hasChildren;    // 是否有子部门

    // 实体转DTO的转换方法
    public static DepartmentDTO fromEntity(Department dept) {
        DepartmentDTO dto = new DepartmentDTO();
        dto.setDepartmentID(dept.getDepartmentID());
        dto.setDepartmentName(dept.getDepartmentName());
        dto.setDepartmentCode(dept.getDepartmentCode());
        dto.setStatus(dept.getStatus());

        // 处理负责人信息
        if (dept.getDepartmentHeadID() != null) {
            dto.setDepartmentHead(dept.getDepartmentHeadID().getRealName());
        } else {
            dto.setDepartmentHead("未设置");
        }

        // 判断是否有子部门
        dto.setHasChildren(!dept.getSubDepartments().isEmpty());

        return dto;
    }

    // 批量转换方法
    public static List<DepartmentDTO> fromEntities(List<Department> departments) {
        return departments.stream()
                .map(DepartmentDTO::fromEntity)
                .collect(Collectors.toList());
    }
}
